home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
bisonpcb.zip
/
READER.C
< prev
next >
Wrap
Text File
|
1987-02-13
|
36KB
|
1,667 lines
/* Input parser for bison
Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
BISON is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY. No author or distributor accepts responsibility to anyone
for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing.
Refer to the BISON General Public License for full details.
Everyone is granted permission to copy, modify and redistribute BISON,
but only under the conditions described in the BISON General Public
License. A copy of this license is supposed to have been given to you
along with BISON so you can know your rights and responsibilities. It
should be in a file named COPYING. Among other things, the copyright
notice and this notice must be preserved on all copies.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding! */
/* read in the grammar specification and record it in the format described in gram.h.
All guards are copied into the fguard file and all actions into faction,
in each case forming the body of a C function (yyguard or yyaction)
which contains a switch statement to decide which guard or action to execute.
The entry point is reader(). */
/*
* Port to PC by Whit Gregg
* Nourse, Gregg & Browne, Inc.
* 1 Horizon Road
* Fort Lee, NJ 07024
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include "files.h"
#include "new.h"
#include "symtab.h"
#include "lex.h"
#include "gram.h"
#include "func.h"
#define LTYPESTR "\n#ifndef YYLTYPE\ntypedef\n struct yyltype\n\
{\n int timestamp;\n int first_line;\n int first_column;\n\
int last_line;\n int last_column;\n char *text;\n }\n\
yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
/* Number of slots allocated (but not necessarily used yet) in `rline' */
int rline_allocated;
extern int definesflag;
extern bucket *symval;
extern int numval;
typedef
struct symbol_list {
struct symbol_list *next;
bucket *sym;
bucket *ruleprec;
}
symbol_list;
int lineno;
bucket *symval;
symbol_list *grammar;
int start_flag;
bucket *startval;
char **tags;
static int typed; /* nonzero if %union has been seen. */
static int lastprec; /* incremented for each %left, %right or
* %nonassoc seen */
static int gensym_count; /* incremented for each generated symbol */
static bucket *errtoken;
static FILE *fattrs1;
void
reader()
{ /* WG */
start_flag = 0;
startval = NULL; /* start symbol not specified yet. */
translations = 0; /* initially assume token number translation
* not needed. */
nsyms = 1;
nvars = 0;
nrules = 0;
nitems = 0;
rline_allocated = 10;
rline = NEW2(rline_allocated, short);
typed = 0;
lastprec = 0;
gensym_count = 0;
semantic_parser = 0;
pure_parser = 0;
grammar = NULL;
/*
* fattrs1 = ftable; JF use fattrs instead /* Unless/until fattrs
* is opened, use ftable instead.
*/
fattrs1 = fattrs;
init_lex();
lineno = 1;
/* initialize the symbol table. */
tabinit();
/* construct the error token */
errtoken = getsym("error");
errtoken->class = STOKEN;
/* construct a token that represents all undefined literal tokens. */
/* it is always token number 2. */
getsym("$illegal.")->class = STOKEN;
/*
* Read the declaration section. Copy %{ ... %} groups to ftable or
* fattrs file. Also notice any %token, %left, etc. found there.
*/
fprintf(ftable, "\n/* A Bison parser, made from %s */\n\n", infile);
read_declarations();
/* output the definition of YYLTYPE into the fattrs or ftable file. */
output_ltype();
/* start writing the guard and action files, if they are needed. */
output_headers();
/*
* read in the grammar, build grammar in list form. write out guards
* and actions.
*/
readgram();
/* write closing delimiters for actions and guards. */
output_trailers();
/*
* assign the symbols their symbol numbers. Write #defines for the
* token symbols into fdefines if requested.
*/
packsymbols();
/* convert the grammar into the format described in gram.h. */
packgram();
/*
* free the symbol table data structure since symbols are now all
* referred to by symbol number.
*/
free_symtab();
}
/* read from finput until %% is seen. Discard the %%.
Handle any % declarations,
and copy the contents of any %{ ... %} groups to ftable or fattrs. */
void
read_declarations()
{ /* WG */
register int c;
register int tok;
for (;;) {
c = skip_white_space();
if (c == '%') {
tok = parse_percent_token();
switch (tok) {
case TWO_PERCENTS:
return;
case PERCENT_LEFT_CURLY:
copy_definition();
break;
case TOKEN:
parse_token_decl(STOKEN, SNTERM);
break;
case NTERM:
parse_token_decl(SNTERM, STOKEN);
break;
case TYPE:
parse_type_decl();
break;
case START:
parse_start_decl();
break;
case UNION:
parse_union_decl();
break;
case LEFT:
parse_assoc_decl(LEFT_ASSOC);
break;
case RIGHT:
parse_assoc_decl(RIGHT_ASSOC);
break;
case NONASSOC:
parse_assoc_decl(NON_ASSOC);
break;
case SEMANTIC_PARSER:
semantic_parser = 1;
open_extra_files();
fattrs1 = fattrs;
break;
case PURE_PARSER:
pure_parser = 1;
break;
default:
fatal("junk after % in definition section");
}
}
else if (c == EOF)
fatal("no input grammar");
else /* JF changed msg */
fatals("Unrecognized char '%c' in declaration section", c);
}
}
/* copy the contents of a %{ ... %} into the definitions file.
The %{ has already been read. Return after reading the %}. */
void
copy_definition()
{ /* WG */
register int c;
register int match;
register int ended;
register int after_percent; /* -1 while reading a character if
* prev char was % */
fprintf(fattrs1, "#line %d \"%s\"\n", lineno, infile);
after_percent = 0;
c = getc(finput);
for (;;) {
switch (c) {
case '\n':
putc((char) c, fattrs1); /* WG */
lineno++;
break;
case '%':
after_percent = -1;
break;
case '\'':
case '"':
match = c;
putc((char) c, fattrs1); /* WG */
c = getc(finput);
while (c != match) {
if (c == EOF || c == '\n')
fatal("unterminated string");
putc((char) c, fattrs1); /* WG */
if (c == '\\') {
c = getc(finput);
if (c == EOF || c == '\n')
fatal("unterminated string");
putc((char) c, fattrs1); /* WG */
if (c == '\n')
lineno++;
}
c = getc(finput);
}
putc((char) c, fattrs1); /* WG */
break;
case '/':
putc((char) c, fattrs1); /* WG */
c = getc(finput);
if (c != '*')
continue;
putc((char) c, fattrs1); /* WG */
c = getc(finput);
ended = 0;
while (!ended) {
if (c == '*') {
while (c == '*') {
putc((char) c, fattrs1); /* WG */
c = getc(finput);
}
if (c == '/') {
putc((char) c, fattrs1); /* WG */
ended = 1;
}
}
else if (c == '\n') {
lineno++;
putc((char) c, fattrs1); /* WG */
c = getc(finput);
}
else if (c == EOF)
fatal("unterminated comment in %{ definition");
else {
putc((char) c, fattrs1); /* WG */
c = getc(finput);
}
}
break;
case EOF:
fatal("unterminated %{ definition");
default:
putc((char) c, fattrs1); /* WG */
}
c = getc(finput);
if (after_percent) {
if (c == '}')
return;
putc('%', fattrs1);
}
after_percent = 0;
}
}
/*